package br.ufrgs.meetapp.utils;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import br.ufrgs.meetapp.activities.LoginActivity;
import br.ufrgs.meetapp.database.PositionMessageDAO;
import br.ufrgs.meetapp.enums.SettingsComponent;
import br.ufrgs.meetapp.models.MFTSettings;
import br.ufrgs.meetapp.models.SettingsEntry;

public class UserPreferencesPersistence {

	private static final String SHARED_PREF = "MFTPrefsFile";
	private static final String SHARED_PREF_USERNAME = "username";
	private static final String SHARED_PREF_PASSWORD = "password";
	private static final String SHARED_PREF_CUSTOMER = "customer";
	private static final String SHARED_PREF_LOGIN_TYPE = "logintype";
	private static final String SHARED_PREF_LOGIN_SAVE_LOGIN = "savelogin";
	private static final String SHARED_PREF_LOGIN_ONCE = "loginonce";
	private static final String SHARED_PREF_LAST_TIMER_EVENT_TIMESTAMP = "lasttimereventtimestamp";
	private static final String SHARED_PREF_SERVER_CONNECTION = "serverconnection";
	private static final String SHARED_PREF_IS_ONLY_TRACK_OTHERS = "isonlytrackothers";
	private static final String SHARED_PREF_SETTINGS_PRESET_SELECTED = "settingspresetselected";

	private static SharedPreferences.Editor editor;
	private static SharedPreferences settings;
	
	/** Type of login, possible values: <br><br><b> "LoginActivity.LOGIN_TYPE_INVALID" - </b>invalid value;<br><b> "LoginActivity.LOGIN_TYPE_CONSUMER" - </b>for consumer;<br><b> "LoginActivity.LOGIN_TYPE_ENTERPRISE" - </b>for enterprises; */
	public static int getLoginType(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SHARED_PREF_LOGIN_TYPE, LoginActivity.LOGIN_TYPE_INVALID);		
	}
	
	/** Type of login, possible values: <br><br><b> "LoginActivity.LOGIN_TYPE_INVALID" - </b>invalid value;<br><b> "LoginActivity.LOGIN_TYPE_CONSUMER" - </b>for consumer;<br><b> "LoginActivity.LOGIN_TYPE_ENTERPRISE" - </b>for enterprises; */
	public static boolean setLoginType(Context context, int loginType) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putInt(SHARED_PREF_LOGIN_TYPE, loginType);
		return editor.commit();		
	}
	
	public static boolean getLoginOnce(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SHARED_PREF_LOGIN_ONCE, false);		
	}
	
	public static boolean setLoginOnce(Context context, boolean loginOnce) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SHARED_PREF_LOGIN_ONCE, loginOnce);
		return editor.commit();
	}
	
	public static boolean getSaveLogin(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SHARED_PREF_LOGIN_SAVE_LOGIN, false);
	}
	
	public static boolean setSaveLogin(Context context, boolean saveLogin) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SHARED_PREF_LOGIN_SAVE_LOGIN, saveLogin);
		return editor.commit();
	}
	
	public static String getUserName(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getString(SHARED_PREF_USERNAME, "");
	}
	
	public static boolean setUserName(Context context, String userName) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putString(SHARED_PREF_USERNAME, userName);
		return editor.commit();
	}
	
	public static String getPassword(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getString(SHARED_PREF_PASSWORD, "");
	}
	
	public static boolean setPassword(Context context, String password) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putString(SHARED_PREF_PASSWORD, password);
		return editor.commit();
	}
	
	public static String getCustomer(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getString(SHARED_PREF_CUSTOMER, "");
	}
	
	public static boolean setCustomer(Context context, String customer) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putString(SHARED_PREF_CUSTOMER, customer);
		return editor.commit();
	}
	
	/** Check this again, the formula might be wrong */
	public static int getTimeInterval(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.TIME_INTERVAL.getSharedPreferencesLabel(), MFTSettings.DEFAULT_TIME_INTERVAL_MILLIS);
	}
	
	/*
	public static boolean setTrackerTimeInterval(Context context, long trackerTimeInterval) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putLong(SHARED_PREF_TRACKER_TIME_INTERVAL, trackerTimeInterval);
		return editor.commit();
	}*/
	
	public static boolean getIsTrackerOff(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SettingsComponent.TRACKER_OFF.getSharedPreferencesLabel(), MFTSettings.DEFAULT_IS_TRACKER_OFF);
	}
	
	public static boolean setIsTrackerOff(Context context, boolean isTrackerOff) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SettingsComponent.TRACKER_OFF.getSharedPreferencesLabel(), isTrackerOff);
		return editor.commit();
	}
	
	public static boolean getIsTrackOthersOn(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SettingsComponent.TRACKER_OTHERS_ON.getSharedPreferencesLabel(), MFTSettings.DEFAULT_IS_TRACKER_OTHERS_ON);
	}
	
	public static boolean setIsTrackOthersOn(Context context, boolean isTrackerOn) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SettingsComponent.TRACKER_OTHERS_ON.getSharedPreferencesLabel(), isTrackerOn);
		return editor.commit();
	}
	
	public static boolean setIsOnlyTrackOthers(Context context, boolean onlyTrackOthers) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SHARED_PREF_IS_ONLY_TRACK_OTHERS, onlyTrackOthers);
		return editor.commit();
	}
	
	public static boolean getIsOnlyTrackOthers(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SHARED_PREF_IS_ONLY_TRACK_OTHERS, true);
	}
	
	public static boolean getServerConnection(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getBoolean(SHARED_PREF_SERVER_CONNECTION, false);
	}
	
	public static boolean setServerConnection(Context context, boolean serverConnection) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean(SHARED_PREF_SERVER_CONNECTION, serverConnection);
		return editor.commit();
	}
	
	public static long getLastTimerEventTimeStamp(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getLong(SHARED_PREF_LAST_TIMER_EVENT_TIMESTAMP, 0);
	}
	
	public static boolean setLastTimerEventTimeStamp(Context context, long lastTimerEventTimeStamp) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putLong(SHARED_PREF_LAST_TIMER_EVENT_TIMESTAMP, lastTimerEventTimeStamp);
		return editor.commit();
	}
	
	public static Location getLastValidLocation(Context context) {
		
		Location lastValidLocation = null;
		
		try {
			PositionMessageDAO positionMessageDAO = new PositionMessageDAO(context);
			
			lastValidLocation = positionMessageDAO.getLastValidLocation();
			
		} catch (Exception exception) {

			System.out.println("Error in UserPreferencesPersistence.getLastValidLocation - ???");
			if (exception.getMessage() != null)
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
		return lastValidLocation;
	}
	
	public static long getLastSentLocationTime(Context context) {
		
		long lastSentLocationTime = 0;
		
		try {
			settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
			lastSentLocationTime = settings.getLong("LAST_SENT_LOCATION_TIME", 0);
			
			
		} catch (Exception exception) {

			System.out.println("Error in UserPreferencesPersistence.getLastSentLocation - trying to open file input or creating the object input stream");
			if (exception.getMessage() != null)
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
		return lastSentLocationTime;
	}
	
	public static boolean setLastSentLocationTime(Context context, long lastSentLocationTime) {

		boolean didSave = false;
		
		try {
			settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
			editor = settings.edit();
			editor.putLong("LAST_SENT_LOCATION_TIME", lastSentLocationTime);
			didSave = editor.commit();
			
		} catch (Exception exception) {

			System.out.println("Error in UserPreferencesPersistence.setLastSentLocation - ??");
			if (exception.getMessage() != null)
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		return didSave;
	}
	
	public static int getDistanceInterval(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.DISTANCE_INTERVAL.getSharedPreferencesLabel(), MFTSettings.DEFAULT_DISTANCE_INTERVAL_METERS);
	}
	
	public static int getCourseRange(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.COURSE_RANGE.getSharedPreferencesLabel(), MFTSettings.DEFAULT_COURSE_RANGE_DEGREES);
	}
	
	public static int getMotionSpeedThreshold(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.MOTION_SPEED_THRESHOLD.getSharedPreferencesLabel(), MFTSettings.DEFAULT_MOTION_SPEED_THRESHOLD_KM_H);
	}
	
	public static int getMotionStopThreshold(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.MOTION_STOP_THRESHOLD.getSharedPreferencesLabel(), MFTSettings.DEFAULT_MOTION_STOP_THRESHOLD_MILLIS);
	}
		
	public static int getSendingInterval(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.SENDING_INTERVAL.getSharedPreferencesLabel(), MFTSettings.DEFAULT_SEND_INTERVAL_MILLIS);
	}
		
	public static int getAcquireTimeout(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SettingsComponent.ACQUIRE_TIMEOUT.getSharedPreferencesLabel(), MFTSettings.DEFAULT_ACQUIREMENT_TIMEOUT_MILLIS);
	}
	
	public static int getPresetSelected(Context context) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		return settings.getInt(SHARED_PREF_SETTINGS_PRESET_SELECTED, 0);
	}
	
	public static boolean setPresetSelected(Context context, int presetSelected) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		editor.putInt(SHARED_PREF_SETTINGS_PRESET_SELECTED, presetSelected);
		return editor.commit();
	}
		
	public static boolean updateSettings(Context context, MFTSettings newSettings) {
		settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
		editor = settings.edit();
		
		
		ArrayList<SettingsEntry> settingsEntryList = newSettings.getSettingsEntryList();
		
		for (SettingsEntry settingsEntry : settingsEntryList) {
			
			if (settingsEntry.getValue() instanceof Boolean) {
				editor.putBoolean(settingsEntry.getKey().getSharedPreferencesLabel(), (Boolean)settingsEntry.getValue());
			}
			if (settingsEntry.getValue() instanceof Integer) {
				editor.putInt(settingsEntry.getKey().getSharedPreferencesLabel(), (Integer)settingsEntry.getValue());
			}
			if (settingsEntry.getValue() instanceof Float) {
				editor.putFloat(settingsEntry.getKey().getSharedPreferencesLabel(), (Float)settingsEntry.getValue());
			}
			if (settingsEntry.getValue() instanceof Long) {
				editor.putLong(settingsEntry.getKey().getSharedPreferencesLabel(), (Long)settingsEntry.getValue());
			}
			if (settingsEntry.getValue() instanceof String) {
				editor.putString(settingsEntry.getKey().getSharedPreferencesLabel(), (String)settingsEntry.getValue());
			}
		}
		
		return editor.commit();
		
	}
	
}
